package com.happycat.service.impl;
 
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happycat.ResponseHeaderModel;
import com.happycat.ResponseModel;
import com.happycat.SysContants;
import com.happycat.mapper.UserPostingMapper;
import com.happycat.mobile.model.UserPosting;
import com.happycat.mobile.service.GenerateIdService;
import com.happycat.mobile.service.UserPostingService;
import com.happycat.utils.ResponseModelUtils;
/**
 * Module: UserPosting.java
 * 
 * @author liaoyong
 * @since JDK 1.8
 * @version 1.0
 * @date 2020-07-29
 * @Descriptions:
 */
@Service
public class UserPostingServiceImpl extends ServiceImpl<UserPostingMapper, UserPosting> implements UserPostingService {
 	
	@Autowired
	private GenerateIdService generateIdService;
	
 	public ResponseModel<UserPosting> selectOne(UserPosting userPosting){
		ResponseModel<UserPosting> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(this.baseMapper.queryDetail(userPosting));
		return responseModel;
 	
 	}
	
	public ResponseHeaderModel	add(UserPosting userPosting){
		ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();
		userPosting.setId(generateIdService.nextDefaultSerialNumber());
		userPosting.setSendTime(new Date());
		boolean result = save(userPosting);
		if(result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("新增成功");
		}else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("新增失败");
		}
		return responseHeaderModel;
	}
	
	public ResponseHeaderModel	modify(UserPosting userPosting){
			ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();
		
		boolean result = updateById(userPosting);
		if(result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("修改成功");
		}else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("修改失败");
		}
		return responseHeaderModel;
	}
	
	public ResponseModel<Page<UserPosting>> pageList(Page<UserPosting>page ){
		ResponseModel<Page<UserPosting>> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(this.baseMapper.pageList(page));
		return responseModel;
	}

	@Override
	public ResponseHeaderModel cancleLike(UserPosting userPosting) {
		QueryWrapper<UserPosting> queryWrapper = new QueryWrapper<UserPosting>(userPosting);
		remove(queryWrapper);
		return ResponseModelUtils.success("取消点赞成功", null);
	}
}
